<?php
namespace yxixia;
class NetUtil{
    
    public static function buildUrl($url,$params){
        if($params){
                if(is_array($params)){
                    $paramStr = http_build_query($params, '', '&');
                }else{
                    $paramStr = $params;
                }
                if(strpos('?', $url)>0){
                    $url = $url.'&'.$paramStr;
                }else{
                    $url = $url.'?'.$paramStr;
                }
            }
            return $url;
    }
	/**
	 * 获取curl
	 * @param int $coin
	 * @return 获取到的信息
	 */
	public  static  function getCurl($url,$params='',$timeout=1) {
            $uri = self::buildUrl($url, $params);
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, $timeout);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_URL, $uri);
            curl_setopt($curl, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36');
            $resp = curl_exec($curl);
            curl_close($curl);

            return $resp;
	}
        
    public static function getCurlWithCookie($url,$cookie,$redirect) {
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36');
                if($cookie){
                    curl_setopt($curl, CURLOPT_COOKIE,$cookie);
                }
                curl_setopt($curl, CURLOPT_ACCEPT_ENCODING,'gzip, deflate');
                curl_setopt($curl, CURLOPT_HEADER, $redirect); 
		$resp = curl_exec($curl);
                $httpCode = curl_getinfo($curl,CURLINFO_HTTP_CODE); 
                curl_close($curl);
                if($httpCode==302&&$redirect){
                    $str =[];
                    preg_match('/^Set-Cookie: (.*)/m',$resp,$str);
                    $ncookie = trim($str[1]).';';  
                    return self::getCurlWithCookie($url, $ncookie, false);
                }
		return $resp;
	}
	
	/**
	 * 发送请求
	 * @param string $url
	 * @param array $params
	 * @param int $timeout
	 * @param string $userAgent
	 */
	public static function postRequest($url, $params = array(), $timeout = 10, $userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69'){
		$paramStr = http_build_query($params, '', '&');
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $paramStr);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); //设置超时为8秒
		curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
		curl_setopt($ch, CURLOPT_HEADER, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。https请求 不验证证书和hosts
		$result = curl_exec($ch);
		$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
		$content = '';
		if($headerSize){
			$content = substr($result, $headerSize);
		}
		$errno = curl_errno($ch);
		if($errno || $http_code != 200){
			$content = false;
		}
		curl_close($ch);
		return $content;
	}
    /**
     * 服务器配置设置
     * **/
    public static function serverSettingRequest($act,$server,$data){
        $url = empty($server['url_s'])?$server['ip_s']:$server['url_s'];
        return self::postRequest($url.'/i.php/setting/index?'.'act='.$act.'&sign='.$server['security_s'],$data,100);
    }
    
}